<?php
//**************************************************************************
//
//            (C) Okulov Rostislav, 2011
//
//**************************************************************************

/*
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 */
  /*
   * Main module. Sort, check, split and show links.
   *
   */

//****************************************************************
//
//         FUNCTIONS
//
//****************************************************************
   include ('mysql_connect.php');

   function mysql_table_seek($tablename, $dbname) {

      $table_list = mysql_query("SHOW TABLES FROM `".$dbname."`");
      while ($row = mysql_fetch_row($table_list)) {

         if ($tablename == $row[0]) {
            return true;
         }

      }
      return false;

   }

   function getlinks_array($filename) {
 
      $data = file_get_contents(htmlspecialchars($filename));

      if (false === $data) {

         echo 'Error reading data.';
         exit;

      }

      preg_match_all("/a[\s]+[^>]*?href[\s]?=[\s\"\']+([http].*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/is", $data, $matches);
    
      return ($matches);
   }

//****************************************************************
//
//         MAIN CODE
//
//****************************************************************

   $match = array();
   $match = getlinks_array($_GET['file']);

   // Connect to MySQL server.
   $link = mysql_connect($dbhost, $dbuser, $dbpassword);
   if (empty($link)) {
      echo 'Не удалось подключиться к MySQL'; 
      exit;
   }
   // Choose database
   mysql_select_db($dbname, $link);
   if (mysql_table_seek('httplink', $dbname)) {

      // we need to drop the table. It should be new every time
      $query = "DROP TABLE httplink";
      mysql_query($query, $link);

   }
   // create table
   $query = "CREATE TABLE httplink (id int(2) primary key auto_increment, name varchar(100), domain varchar(30))";
   mysql_query($query, $link);
   // write array in database
   for ($i=0;$i<count($match[1]);$i++) {

       $query = "INSERT INTO httplink VALUES (0,'".mysql_escape_string($match[1][$i])."','".mysql_escape_string(parse_url($match[1][$i], PHP_URL_HOST))."')";
       mysql_query($query, $link);

   }
   
   $i = 0;
   $query = "SELECT domain FROM httplink GROUP BY domain HAVING count(domain) > 1";
   $result = mysql_query($query, $link);
   while ($line = mysql_fetch_row($result)) {

	 $arr[$i] = $line[0];
         $i++;

   }

   echo count($arr).'|';
 // тут начинается вывод текста, который и будет ответом на ajax запрос.
   $query = "SELECT name FROM httplink GROUP BY domain HAVING count(domain) = 1";
   $result = mysql_query($query, $link);
   while ($line = mysql_fetch_row($result)) {
			 
         echo '<option>'.$line[0].'</option>';

   }
   echo '*';
   for ($h=0;$h<count($arr);$h++) {

       $query = "SELECT name FROM httplink WHERE domain LIKE '%".$arr[$h]."%'";
       $result = mysql_query($query, $link);
       while ($line = mysql_fetch_row($result)) {
			 
             echo '<option>'.$line[0].'</option>';

       }
       echo '*';

   }
?>
